package com.amazon.commerce.service;

import com.amazon.commerce.model.*;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;

/**
 * Bootstraps the Amazon Web Service innovation. Any call to Amazon model should
 * be in this class.
 *
 * @author: Nik Van Looy
 * @since: 19/09/11
 */
public class AwsGateway extends WebServiceGatewaySupport {

    final private String awsAccessKey;

    public AwsGateway(final String awsAccessKey) {
        super();
        this.awsAccessKey = awsAccessKey;
    }

    public ItemSearchResponse search(final ItemSearchRequest... searchRequests) throws RuntimeException {
        ItemSearchResponse response = null;
        try {
            final ItemSearch lookup = new ItemSearch();
            lookup.setAWSAccessKeyId(awsAccessKey);

            for (final ItemSearchRequest itemSearchRequest : searchRequests) {
                lookup.getRequest().add(itemSearchRequest);
            }
            response = (ItemSearchResponse) getWebServiceTemplate().marshalSendAndReceive(lookup);
        } catch (final Exception e) {
            logger.error("Error", e);
        }
        return response;
    }
}
